package com.google.code.jholidays.collections;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.Test;

import com.google.code.jholidays.base.TestHelper;
import com.google.code.jholidays.core.DescriptorCollection;
import com.google.code.jholidays.core.EventDescriptor;


public class DescriptorCollectionsTest extends TestHelper {

    @Test
    public void testDescriptorCollection() {
	DescriptorCollection collection = new DescriptorCollection();
	assertNotNull(collection);
    }

    @Test
    public void testIsConsistent() {
	int[][] table = getConsistentTable();
	DescriptorCollection collection = generateCollection(table);
	assertNotNull(collection);
	assertEquals(collection.size(), table.length);
	assertTrue(collection.isConsistent());
    }

    @Test
    public void testNotConsistentMissingParent() {
	int[][] table = getTableMissingParent();
	DescriptorCollection collection = generateCollection(table);
	assertNotNull(collection);
	assertEquals(collection.size(), table.length);
	assertFalse(collection.isConsistent());
    }

    @Test
    public void testNotConsistentSelfDependency() {
	int[][] table = getTableDependsItself();
	DescriptorCollection collection = generateCollection(table);
	assertNotNull(collection);
	assertEquals(collection.size(), table.length);
	assertFalse(collection.isConsistent());
    }

    @Test
    public void testGetInvalidDescriptors() {
	int[][] table = getTableDependsItself();
	DescriptorCollection collection = generateCollection(table);
	assertNotNull(collection);
	assertEquals(collection.size(), table.length);

	// test dependent itself
	List<EventDescriptor> list = collection.getInvalidDescriptors();
	assertNotNull(list);
	assertEquals(list.size(), 1);

	EventDescriptor evt = list.get(0);
	assertNotNull(evt);
	assertEquals(evt.getID(), evt.getParentID());

	// test missing parent
	table = getTableMissingParent();
	collection = generateCollection(table);
	assertNotNull(collection);
	assertEquals(collection.size(), table.length);

	// test dependent itself
	list = collection.getInvalidDescriptors();
	assertNotNull(list);
	assertEquals(list.size(), 1);

	evt = list.get(0);
	assertNotNull(evt);
	assertEquals(evt.getParentID(), 4);

    }

    private int[][] getConsistentTable() {
	int[][] result = new int[][] { { 1, 0 }, { 2, 1 }, { 3, 1 }, { 4, 2 },
		{ 5, 2 }, { 6, 3 }, { 7, 3 } };
	return result;
    }

    private int[][] getTableMissingParent() {
	int[][] result = new int[][] { { 1, 0 }, { 2, 1 }, { 3, 4 } };
	return result;
    }

    private int[][] getTableDependsItself() {
	int[][] result = new int[][] { { 1, 1 }, { 2, 1 }, { 3, 1 } };
	return result;
    }

}
